var utils = require('../../utils/util.js');
var base64Js = require('../../utils/base64.js');
var md5Js = require('../../utils/md5.js');
var app = getApp();
Page({
    data: {
        activeIndex: 0,
        actionSheetHidden: true,
        sku_info_checkout:[],
        index:0,
        num:1,
        inputShowed: false,
        inputVal: ""
    },
    showInput: function () {
        this.setData({
            inputShowed: true
        });
    },
    hideInput: function () {
        this.setData({
            inputVal: "",
            inputShowed: false
        });
    },
    clearInput: function () {
        this.setData({
            inputVal: ""
        });
    },
    inputTyping: function (e) {
        var product_info = wx.getStorageSync('product_info');
        var key_word = e.detail.value;
        var product_search = [];
        for(var i=0;i<product_info.length; i++){
            var obj = product_info[i];
            if(obj.name.match(key_word)){
                product_search.push(obj);
            }
        }
        if(key_word != false){
            this.setData({
            product_search: product_search
            });
        }
    },

    bindMinus: function(e) {
    var index = parseInt(e.currentTarget.dataset.index);
    var num = this.data.num;
    // 如果只有1件了，就不允许再减了
    if (num > 1) {
      num --;
    }
    // 只有大于一件的时候，才能normal状态，否则disable状态
    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    // 按钮可用状态
    var minusStatuses = this.data.minusStatuses;
    // 将数值与状态写回
    this.setData({
      num: num,
      minusStatuses: minusStatus
    });
  },
  bindPlus: function(e) {
    var index = parseInt(e.currentTarget.dataset.index);
    var num = this.data.num;
    // 自增
    num ++;
    // 只有大于一件的时候，才能normal状态，否则disable状态
    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    // 按钮可用状态
    var minusStatuses = this.data.minusStatuses;
    // 将数值与状态写回
    this.setData({
      num: num,
      minusStatuses: minusStatus
    });
  },

  showModal: function (e) {
    // 显示遮罩层
    var sku_info = JSON.parse(e.currentTarget.dataset.info);console.log(sku_info);
    var product_name = e.currentTarget.dataset.productname;console.log(product_name);
    var productinfo = e.currentTarget.dataset.productinfo;console.log(productinfo);
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      product_name:product_name,
      sku_info:sku_info,
      product_info:productinfo,
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },

//选择属性
 radioChange: function (e) {
    this.setData({
      sku_info_checkout: e.currentTarget.dataset.skuinfo,
      activeIndex: e.currentTarget.dataset.index
    });
  },

  //点击购物车图片时添加商品
  submit: function(e){
    var sku_info = e.currentTarget.dataset.skuinfo;
    var skuinfotmp = e.currentTarget.dataset.skuinfotmp;
    var carts_num = this.data.num;
    if(sku_info == false){
      sku_info = skuinfotmp;
    }
    var product_name = e.currentTarget.dataset.productname;
    var tmp = {sku_itmes:sku_info,name:product_name,cart_num:carts_num};
    //加入购物车
    utils.addCart(tmp);
    //获取缓存的购物车对象信息
    var cart_info =  wx.getStorageSync('cart_info');
    this.setData({cart_num:cart_info.length});
     // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
},
});